var wineApp = wineApp || {};
wineApp.register = (function() {
    //var key = wineApp.CONFIG.STORAGE.PREFIX + '.' + 'userInfo.msg';
    //var userInfo = wineApp.localStorage.get(key);
    //var width = (wineApp.tool.getScreenWidth() * 0.46).toFixed(0);
    var checkVerifyCode = false;
    var initEvent = function() {
        $(document).on('click', '#register', function(event) {
            event.preventDefault();
            var username = $('#username').val();
            var pwd = $('#pwd').val();
            var checkcode = $('#checkcode').val();
            var confirmpwd = $('#confirmpwd').val()
            if (username == '' || pwd == '' || confirmpwd == '') {
                wineApp.tool.toastError('请输入手机号或密码');
                return;
            }
            // if (checkcode == '') {
            //     wineApp.tool.toastError('请输入验证码');
            //     return;
            // }
            if (pwd !== confirmpwd) {
                wineApp.tool.toastError('两次密码不一致！');
                return;
            }
            var check = checkCode(username, checkcode);
            if (!check) {
                wineApp.tool.toastError('验证码有误！');
                return;
            }
            var token = wineApp.ajax.getCert();
            var epwd = CryptoJS.MD5(token + CryptoJS.MD5(pwd)).toString();
            var ajaxUrl = wineApp.CONFIG.API.register;
            wineApp.ajax.post(ajaxUrl, {
                phone: username,
                name: username,
                epwd: pwd
            }).done(function(data) {
                console.log(data);
                console.log('register successful');
                window.location.href = 'login.html'
            }).fail(function(data) {
                wineApp.tool.toastError(data.msg);
            });

        }).on('click', '#getcode', function(event) {
            var username = $('#username').val();
            timer();
            getCode(username);
        }).on('blur', '#checkcode', function(event) {
            var username = $('#username').val();
            var code = $('#code').val();
            //checkVerifyCode = checkCode(username, code);
        });
    }

    var getCode = function(phone) {
        var ajaxUrl = wineApp.CONFIG.API.code;
        wineApp.ajax.post(ajaxUrl, {
            phone: phone
        })
    }

    var checkCode = function(phone, code) {
        var ajaxUrl = wineApp.CONFIG.API.checkcode;
        var checked = false;
        wineApp.ajax.post(ajaxUrl, {
            phone: phone,
            code: code,
            async: false
        }).done(function(data) {
            checked = true;
            console.log('验证码校验成:%O', data);
        })
        return checked;
    }

    var initPage = function() {
        initEvent();
        //timer();
    }
    var obj;
    var timer = function(intDiff, btn) {
        $('#getcode').prop('disabled', true);
        $('#getcode').addClass('disabled');
        var fiveSeconds = new Date().getTime() + 59000;
        $('#getcode').countdown(fiveSeconds, { elapse: false })
            .on('update.countdown', function(event) {
                var $this = $(this);
                $this.html(event.strftime('重新获取 %S秒'));
            }).on('finish.countdown', function(event) {
                var $this = $(this);
                $this.html(event.strftime('获取验证码'));
                $('#getcode').prop('disabled', false);
                $('#getcode').removeClass('disabled');
            });
        // clearInterval(obj);
        // var tmp = intDiff;
        // obj = window.setInterval(function() {
        //     if (tmp == 0) {
        //         $(btn).attr("disabled", false);
        //         $(btn).text("获取验证码")
        //         tmp = 0;
        //     } else {
        //         $(btn).attr("disabled", true);
        //         var text = "重新发送(" + tmp + ")";
        //         $(btn).text(text)
        //         tmp--;
        //     }

        // }, 1000)

    }

    return {
        initPage: initPage
    }
})()

$(document).ready(function() {
    $('.wine_footer').remove();
    wineApp.common.initHeaderIcon(0, true, {
        camera: false,
        search: false
    });
    wineApp.register.initPage();
});